<?php


namespace App\Models;


use QL\QueryList;

class Filter
{
    public static function str_replace($str)
    {
        return str_replace(['深圳','沙漠风'],['郑州','速维'], $str);
    }

    public static function ext($path)
    {
        $path = pathinfo($path);
        $ext = strpos('?', $path['extension']) !== false?strstr($path['extension'], '?', true):$path['extension'];
        return $ext;
    }

    public static function resourceLink($url, $type = '', $path = '')
    {
        $domain = pathinfo($url,PATHINFO_DIRNAME);
        $ql = QueryList::get($url)
        ->rules([
        'link'=>array('link','href'),
        'script'=>array('script','src'),
        'img'=>array('img','src'),
        ])
        ->query()->getData();
        //print_r($ql);die();
        foreach($ql as $val){
            if(!empty($val['link'])){
                $path = pathinfo($val['link']);
                $path = 'static'.$path['dirname'].'/';
                $downurl = $domain.$val['link'];
                self::resoureDown($downurl, $path);
                //echo $downurl.PHP_EOL;
            }
            if(!empty($val['script'])){
                $path = pathinfo($val['script']);
                $path = 'static'.$path['dirname'].'/';
                $downurl = $domain.$val['script'];
                //过滤百度和外网链接
                if(strpos($val['script'], '.js') !== false && strpos($val['script'], 'baidu') === false){
                    self::resoureDown($downurl, $path);
                    //echo $downurl.PHP_EOL;
                }
            }
            if(!empty($val['img'])){
                $path = pathinfo($val['img']);
                $path = 'static'.$path['dirname'].'/';
                $downurl = $domain.$val['img'];
                self::resoureDown($downurl, $path);
                //echo $downurl.PHP_EOL;
            }
        }
    }

    public static function resoureDown($url, $path = '')
    {
        $path = public_path($path);
        if(!file_exists($path)){
            mkdir($path, 0777,true);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);
        $filename = pathinfo($url, PATHINFO_BASENAME);
        $filename = strpos($filename, '?') !== false ? strstr($filename,'?',true):$filename;
        $resource = fopen($path . $filename, 'a');
        fwrite($resource, $file);
        fclose($resource);
    }

    public static function strReplace($str)
    {
        $str = str_replace(
            ['深圳', '沙漠风', 'https://www.szweb.cn', 'http://www.szweb.cn',
                '/webcss',
                '/webjs',
                'src="/newres',
                'src="/UpLoadFiles',
                'src="/Uploads',
                'src="/webimages',
                'src="/KindEditor',
                '0755-83152876',
            ],
            ['{{$name[\'addr\']}}', '{{$name[\'name\']}}', '{{$domain}}', '{{$domain}}',
                '/static/webcss',
                '/static/webjs',
                'src="/static/newres',
                'src="/static/UpLoadFiles',
                'src="/static/Uploads',
                'src="/static/webimages',
                'src="/static/KindEditor',
                '{{$tel}}',
            ],
            $str
        );
        return $str;
    }

    public static function cutStr($url, $start, $end, $save_path)
    {
        $str =  file_get_contents($url);
        $rs = mb_strstr($str, $start);
        $str = mb_strstr($rs, $end , true);
        $path = base_path().'/resources/views/';

        $str = self::strReplace($str);
        file_put_contents($path.$save_path.'.blade.php', '@extends(\'layouts.public\')'.PHP_EOL.'@section(\'middle\')'.PHP_EOL.'@include(\'middle.wangzhanjianshe\')'.PHP_EOL.'@endsection'.PHP_EOL.'@section(\'content\')'.PHP_EOL.$str.PHP_EOL.'@endsection');
      /*  $str =  file_get_contents('https://www.szweb.cn/wangzhanjianshe/keywordseo/');
        $rs = mb_strstr($str, '<div class="full-case-crumbs">');
        $str = mb_strstr($rs, '<div class="full-icontact-cover m-ft-contact">' , true);
        $path = base_path().'/resources/views/build/';
        $str = str_replace(
            ['深圳', '沙漠风', 'https://www.szweb.cn', 'http://www.szweb.cn'],
            ['{{$name[\'addr\']}}', '{{$name[\'name\']}}', '{{$domain}}', '{{$domain}}'],
            $str
        );
        file_put_contents($path.'keywordseo.blade.php', $str);*/
    }


    public static function download($url, $path = 'logo')
    {
        $save_path = '/uploads/'.$path.'/';
        $path = public_path($save_path);
        if(!file_exists($path)){
            mkdir($path, 0777,true);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);
        $filename = pathinfo($url, PATHINFO_BASENAME);
        $resource = fopen($path . $filename, 'a');
        fwrite($resource, $file);
        fclose($resource);
        return $save_path.$filename;
    }

    public static function createCate($name, $data)
    {
        $cate = Category::where('name', $name)->first();
        if (!$cate) {
            $cate = new Category();
            $cate->name = $name;
            if(isset($data['img'])){
                $cate->img = $data['img'];
            }
            if(isset($data['long_name'])){
                $cate->long_name = $data['long_name'];
            }
            $cate->title = self::str_replace($data['title']);
            $cate->keywords = self::str_replace($data['keywords']);
            $cate->description = self::str_replace($data['description']);
            $cate->save();
        }
        return $cate;
    }
}